<?php

/*
	Class: EntitytypeController.php

	About: Author
	J William McCarthy

	About: License
	<http://communit.as/docs/license>
*/
class Content_EntitytypeController extends App_Controller_Action_Abstract {

	/* Group: Instance Methods */

	/*
		Function: init
			Invoked automatically when an instance is created.
			Initializes the current instance.
			Also initializes the parent object (calls init() on the parent instance).
	*/
	function init(){
		parent::init();
	}

	/* Group: Actions */

	/*
		Function: index
			Lists all available entity types, links to create/edit
		
	*/
	function indexAction(){
	    $entity_types_table = new ContentEntityTypes();
	    $entity_types = $entity_types_table->fetchAll(null,"entity_type_id");
	    
	    if(!is_null($entity_types)){	    	
	    	$this->view->entity_types = $entity_types->toArray();
	    	
	    }
	}
	/*
		Function: edit
			Used to create or edit entity types.
		
	*/	
	function editAction(){
		$entity_types_table = new ContentEntityTypes();		
		$request = new App_Request($this->getRequest());
		
		
		if($this->getRequest()->isPost()){
		
			$errors = array();
			if($entity_types_table->entityTypeIdExists($request->entity_type_id)){
				$errors[] = "Entity type id in use.";
			}
			
        	$entity_type_id_validator = new Zend_Validate();
			$entity_type_id_validator->addValidator(new Zend_Validate_StringLength(1, 64));
			$entity_type_id_validator->addValidator(new Zend_Validate_Alnum());

			if(!$request->has("id")){
				if (!$entity_type_id_validator->isValid($request->entity_type_id)){
					$errors[] = "Entity type id must be alphanumeric and between 1 and 64 characters.";
				}
			}
			
			$entity_type_name_validator = new Zend_Validate();
			$entity_type_name_validator->addValidator(new Zend_Validate_StringLength(1,255));
			
			if(!$entity_type_name_validator->isValid($request->entity_type_name)){
				$errors[] = "Entity type name must be between 1 and 255 characters.";	
			}
			
			if(count($errors) == 0){		
				$data = array(
					
					'entity_type_name'			=> $request->entity_type_name,
					'entity_type_description'	=> $request->entity_type_description,
				);
				
				if($request->has('inherits_entity_type_id')){
					$data['inherits_entity_type_id'] = $request->inherits_entity_type_id;
				}
				
				if($request->has("id")){
					$where = $entity_types_table->getAdapter()->quoteInto('entity_type_id = ?',$request->id);
					$entity_types_table->update($data,$where);
				} else {
					$data['entity_type_id'] = $request->entity_type_id;
					$entity_types_table->insert($data);
				}
				
				$this->_redirect("/content/entitytype");
			} else {
				$this->view->errors = $errors;
				$this->view->entity_type_id 			= $request->entity_type_id;
				$this->view->entity_type_name 			= $request->entity_type_name;
				$this->view->entity_type_description	= $request->entity_type_description;
				$this->view->inherits_entity_type_id	= $request->inherits_entity_type_id;
			} 
			
			
		} else {
			
			if($request->has('id')){
				
				$entity_type = $entity_types_table->getEntityTypeById($request->id);
				
				if(!is_null($entity_type)){
					$this->view->entity_type_id 			= $entity_type->entity_type_id;
					$this->view->entity_type_name 			= $entity_type->entity_type_name;
					$this->view->entity_type_description	= $entity_type->entity_type_description;
					$this->view->inherits_entity_type_id	= $entity_type->inherits_entity_type_id;
					$this->view->id = $request->id;
				}
			}
		}
		
	}
	
	function deleteAction(){
		
	}
	
}